package com.utf999.qiangmanger.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.AbstractRelationalDBDAO;
import com.utf999.qiangmanger.dao.iface.IOrderTypeDAO;
import com.utf999.qiangmanger.entity.orm.OrderTypeEntity;
import com.utf999.qiangmanger.exception.BizException;

/**
 * @author yinwenjie
 *
 */
@Component("OrderTypeDAOImpl")
public class OrderTypeDAOImpl extends AbstractRelationalDBDAO<OrderTypeEntity> implements IOrderTypeDAO {

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.AbstractRelationalDBDAO#getEntityClass()
	 */
	@Override
	protected Class<OrderTypeEntity> getEntityClass() {
		return OrderTypeEntity.class;
	} 

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderTypeDAO#queryOrderTypesByCompanyid(java.lang.String)
	 */
	//TODO 还没有进行SQL优化。
	@Override
	public List<OrderTypeEntity> queryOrderTypesByCompanyid(String companyid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		
		List<OrderTypeEntity> types = this.queryByHqlFile("IOrderTypeDAO.queryOrderTypesByCompanyid", condition);
		return types;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderTypeDAO#queryOrderTypeByCode(java.lang.String, java.lang.String)
	 */
	@Override
	public OrderTypeEntity queryOrderTypeByCode(String companyid, String orderCode) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("orderCode", orderCode);
		
		List<OrderTypeEntity> types = this.queryByHqlFile("IOrderTypeDAO.queryOrderTypeByCode", condition);
		if(types == null || types.size() == 0) {
			return null;
		}
		return types.get(0);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderTypeDAO#existOrderTypeByCode(java.lang.String, java.lang.String)
	 */
	@Override
	public OrderTypeEntity existOrderTypeByCode(String companyid, String orderCode) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("companyid", companyid);
		condition.put("orderCode", orderCode);
		
		List<OrderTypeEntity> types = this.queryByHqlFile("IOrderTypeDAO.existOrderTypeByCode", condition);
		if(types == null || types.size() == 0) {
			return null;
		}
		return types.get(0);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderTypeDAO#bindUsergroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void bindUsergroupMapping(String ordertypeid, String... groupids) throws BizException {
		for (String groupid : groupids) {
			HashMap<String, Object> condition = new HashMap<String, Object>();
			condition.put("ordertypeid", ordertypeid);
			condition.put("usergroupid", groupid);
			
			this.executeSQLFile("IOrderTypeDAO.bindUsergroupMapping", condition);
		}
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderTypeDAO#unbindUsergroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void unbindUsergroupMapping(String ordertypeid, String... groupids) throws BizException {
		for (String groupid : groupids) {
			HashMap<String, Object> condition = new HashMap<String, Object>();
			condition.put("ordertypeid", ordertypeid);
			condition.put("usergroupid", groupid);
			
			this.executeSQLFile("IOrderTypeDAO.unbindUsergroupMapping", condition);
		}
	}
}
